package cn.zmit.kuxi.widget;

import cn.zmit.kuxi.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

/**
 * 自定义Checkbox，继承自ImageView，解决在ListView中冲突错位问题
 * @author Robin
 * time 2015-04-20 09:38:33
 *
 */
public class CheckBoxWithListVview extends ImageView {
	
	private OnCheckedChangeListener mOnCheckedChangeListener;
	
    private boolean mChecked=false;

	public CheckBoxWithListVview(Context context) {
		super(context);
		init(context);
	}


	public CheckBoxWithListVview(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public CheckBoxWithListVview(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	

	private void init(Context context) {
		updateState(mChecked);
		setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				toggle();
			}
		});
	}

    /**
     * 更新Checkbox状态显示
     */
	public void updateState(boolean checked) {
		if (checked) {
			setBackgroundResource(R.drawable.button_check_in);
		}else {
			setBackgroundResource(R.drawable.button_check_whit);
		}
		mChecked=checked;
	}


	public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            // 更新状态
            updateState(mChecked);
            
            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.onCheckedChanged(this, checked);
            }
        }
	}


	public boolean isChecked() {
		return mChecked;
	}


	public void toggle() {
        setChecked(!mChecked);
	}
	
	/**
	 * 设置check状态变化回调
	 * @param listener
	 */
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mOnCheckedChangeListener = listener;
    }
	
	/**
	 * check状态变化回调接口
	 * @author Robin
	 * time 2015-04-20 09:27:00
	 */
    public static interface OnCheckedChangeListener {
        void onCheckedChanged(CheckBoxWithListVview checkBox, boolean isChecked);
    }

}
